﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

using CPPEI.Extensions;

namespace XWeb.Core
{
    public static class Cache
    {
        private static Data.App s_curentApp;

        public static Data.App CurrentApp
        {
            get
            {
                return s_curentApp;
            }
        }

        public static void Initiate(string menuFile)
        {
            string logo = System.Configuration.ConfigurationManager.AppSettings["XWeb.App.Logo"];
            string name = System.Configuration.ConfigurationManager.AppSettings["XWeb.App.Name"];
            string baseUrl = System.Configuration.ConfigurationManager.AppSettings["XWeb.App.BaseUrl"];

            s_curentApp = new Data.App();
            s_curentApp.Logo = logo;
            s_curentApp.Name = name;
            s_curentApp.BaseUrl = baseUrl;

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(menuFile);

            XmlNodeList xMenuItems = xDoc.DocumentElement.GetNodes("menu");
            foreach (XmlNode xMenuItem in xMenuItems)
            {
                s_curentApp.Menus.Add(Convert(xMenuItem));
            }
        }

        private static Data.Menu Convert(XmlNode xMenu)
        {
            Data.Menu menu = new Data.Menu();

            menu.Name = xMenu.GetAttributeValue("name");
            menu.Icon = xMenu.GetAttributeValue("icon");
            menu.Url = xMenu.GetAttributeValue("url");

            XmlNodeList xSubMenuItems = xMenu.GetNodes("menu");
            foreach (XmlNode xSubMenuItem in xSubMenuItems)
            {
                menu.Children.Add(Convert(xSubMenuItem));
            }
            return menu;
        }
    }
}
